<?php
include("inc/class.otr.php");
$otr = new OTR();

if(!$otr->checkLogin()){
	header("Location: index.php");
}

if(isset($_REQUEST['id'])){

	$project_id = "*NEW*";
	$project_shortname = "";
	$project_description = "";
	$project_local = "";
	$project_site = "";
	$project_customer = "";
	$project_started = "";
	$project_ended = "";
	$project_completed = "";
	$project_mysqlhost = "";
	$project_mysqldb = "";
	$project_mysqluser = "";
	$project_mysqlpass = "";
	$project_phpmyadminhost = "";
	$project_phpmyadminuser = "";
	$project_phpmyadminpass = "";
	$project_ftphost = "";
	$project_ftpuser = "";
	$project_ftppass = "";
	
	if($_REQUEST['id']!=0){
		$project = $otr->getProject($_REQUEST['id']);
		//print_r($project);
		$project_id = $project['id'];
		$project_shortname = $project['shortname'];
		$project_description = $project['description'];
		$project_local = $project['local'];
		$project_site = $project['site'];
		$project_customer = $project['customer_id'];
		$project_started = $project['started_date'];
		$project_ended = $project['completed_date'];
		$project_completed = $project['completed'];

		$project_mysqlhost = $project['mysql_host'];
		$project_mysqldb = $project['mysql_db'];
		$project_mysqluser = $project['mysql_user'];
		$project_mysqlpass = $project['mysql_pass'];
		$project_phpmyadminhost = $project['phpmyadmin_host'];
		$project_phpmyadminuser = $project['phpmyadmin_user'];
		$project_phpmyadminpass = $project['phpmyadmin_pass'];
		$project_ftphost = $project['ftp_host'];
		$project_ftpuser = $project['ftp_user'];
		$project_ftppass = $project['ftp_pass'];
	}
}else{
	echo("<pre>");
	print_r($_REQUEST);
	echo("</pre>");
	//assume save or insert
	$res = $otr->setProject($_REQUEST['txt_id'], $_REQUEST['txt_shortname'], $_REQUEST['txt_description'], $_REQUEST['txt_local'], $_REQUEST['txt_site'], $_REQUEST['ddl_customer'], $_REQUEST['txt_starteddate'], $_REQUEST['txt_completeddate'], $_REQUEST['txt_completed']);
	echo($res);
	exit();
	
	/*$project_id = $_REQUEST['txt_id'];
	$project_shortname = $_REQUEST['txt_shortname'];
	$project_description = $_REQUEST['txt_description'];
	$project_local = $_REQUEST['txt_local'];
	$project_site = $_REQUEST['txt_site'];
	$project_customer = $_REQUEST['ddl_customer'];
	$project_started = $_REQUEST['txt_starteddate'];
	$project_ended = $_REQUEST['txt_completeddate'];
	$project_completed = $_REQUEST['chk_completed'];*/
}
$users = $otr->getParticipants($project);

function buildRoleOptions($roleId, $roleArray){
	$roleOptions = '<option value="0">[select role]</option>';
	foreach($roleArray as $key=>$role){
		$selected = '';
		if($roleId == $key){
			$selected = "selected='selected'";
		}
		$roleOptions .= "<option ".$selected." value='".$key."'>".$role."</option>"; 
	}
	return $roleOptions;
	//echo($roleOptions);
}
?>
<div id="project" style="float: left;">
<form id="frm_project" method="post" action="project.php">
<table>
	<tbody>
		<tr>
			<td style="vertical-align: top;">
				<table>
					<tbody>
						<tr>
							<td class="label"><label for="txt_id">id:</label></td>
							<td><input id="txt_id" name="txt_id" type="text" readonly="readonly" value="<?php echo($project_id);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_shortname">shortname:</label></td>
							<td><input type="text" id="txt_shortname" name="txt_shortname" value="<?php echo($project_shortname);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_description">description:</label></td>
							<td><input type="text" id="txt_description" name="txt_description" value="<?php echo($project_description);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_local">local:</label></td>
							<td><input type="text" id="txt_local" name="txt_local" value="<?php echo($project_local);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_site">site:</label></td>
							<td><input type="text" id="txt_site" name="txt_site" value="<?php echo($project_site);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="ddl_customer">customer:</label></td>
							<td>
								<select id="ddl_customer" name="ddl_customer">
						<!-- FETCH customers -->
					<?php
						$customers = $otr->getCustomers();
						foreach($customers as $c){
							$selected = '';
							if($c['id']==$project_customer) $selected="selected='selected'";
					?>
								<option <?php echo($selected);?> value="<?php echo($c['id']);?>"><?php echo($c['company']);?></option>
					<?php
						}
					?>
								</select>
							</td>
						</tr>
						<tr>
							<td class="label"><label for="txt_starteddate">Started @</label></td>
							<td><input type="text" id="txt_starteddate" name="txt_starteddate" value="<?php echo($project_started);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_completeddate">Ended @</label></td>
							<td><input type="text" readonly="readonly" id="txt_completeddate" name="txt_completeddate" value="<?php echo($project_completed);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="chk_completed">Completed</label></td>
							<td><input type="checkbox" id="chk_completed" name="chk_completed" style="width: 30px;" value="<?php echo($project_completed);?>" onclick="setCheckbox($('txt_completed'), this.checked);" />
							<input type="hidden" id="txt_completed" name="txt_completed" value="<?php echo($project_completed);?>" /></td>
						</tr>
						<tr>
							<td></td>
							<td>
								<input type="button" value="save" onclick="saveProject();"/>
							</td>
						</tr>
						<tr>
							<td></td>
							<td>
								<div id="pnl_result"></div>
							</td>
						</tr>
					</tbody>
				</table>
			</td>
			<td style="vertical-align: top;">
				<table>
					<tbody>
						<tr>
							<td class="label"><label for="txt_mysqlhost">mysql host:</label></td>
							<td><input type="text" id="txt_mysqlhost" name="txt_mysqlhost" value="<?php echo($project_mysqlhost);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_mysqldb">mysql db:</label></td>
							<td><input type="text" id="txt_mysqldb" name="txt_mysqldb" value="<?php echo($project_mysqldb);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_mysqluser">mysql user:</label></td>
							<td><input type="text" id="txt_mysqluser" name="txt_mysqluser" value="<?php echo($project_mysqluser);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_mysqlpass">mysql pass:</label></td>
							<td><input type="text" id="txt_mysqlpass" name="txt_mysqlpass" value="<?php echo($project_mysqlpass);?>" /></td>
						</tr>
						<tr>
							<td class="label"><label for="txt_mysqlhost">mysql host:</label></td>
							<td><input type="text" id="txt_mysqlhost" name="txt_mysqlhost" value="<?php echo($project_mysqlhost);?>" /></td>
						</tr>
					</tbody>
				</table>
			</td>
			<td style="vertical-align: top; border: 1px solid gray;">
				<table>
					<tbody>
						<tr>
							<td>Users contributing to project
							<input type="hidden" value="" id="txt_UserRoles" name="txt_UserRoles" />
							</td>
						</tr>
<?php
						foreach($users as $user){
?>
							<tr>
								<td><?php echo($user['fullname']); ?></td>
								<td style="text-align: right;">
									<select style="width: 90px;" name="ddl_user" onchange="dumpUserRole(<?php echo($user['id']);?>, this);">
<?php
										echo(buildRoleOptions($user['role_id'], $roleIds));
?>
									</select>
								</td>
							</tr>
<?php
						}
?>
					</tbody>
				</table>
			</td>
		</tr>
	</tbody>
</table>
</form>
</div>
<script type="text/javascript">
	var userroles;
	var saveProject = function(){
		ajaxPost($('frm_project'), "project.php", $('pnl_result'), null);
	}
	var setCheckbox = function(el, value){
		if(value){
			el.value = 1;
		}else{
			el.value = 0;			
		}
	}
	var dumpUserRole = function(userId, ddl){
		$('txt_UserRoles').value = 
	}
</script>